<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm"  :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="水印代码" prop="sydm">
        <el-input v-model="queryParams.sydm" placeholder="请输入水印代码" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="设置" prop="setup" >
        <el-select v-model="queryParams.setup" style="width: 100%;" @change="querySetupChange(queryParams)">
          <el-option v-for="dict in dict.type.ARCHIVE_YQSY_SETUP" :key="dict.value" :label="dict.label" :value="dict.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="水印内容" prop="synr" v-if="queryParams.setup==null">
        <el-input ref="synrInput" placeholder="请先选择设置" disabled="disabled"/>
      </el-form-item>
      <el-form-item label="水印内容" prop="synr" v-else-if="queryParams.setup!=1">
        <el-input ref="synrInput" v-model="queryParams.synr" placeholder="请输入水印内容"/>
      </el-form-item>
      <el-form-item label="水印内容" prop="synr" v-else>
        <el-select ref="synrSelect" v-model="queryParams.synr" style="width: 100%;">
          <el-option v-for="dict in dict.type.ARCHIVE_YQSY_TRENDS" :key="dict.value" :label="dict.label" :value="dict.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"

          @click="handleAdd"
          v-hasPermi="['archive:yqSy:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          -->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['archive:yqSy:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"

          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['archive:yqSy:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          -->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['archive:YqSy:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="YqSyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" :index="table_index" width="55" align="center" prop="id"/>
      <el-table-column label="设置" align="center" prop="setup" />
      <el-table-column label="水印代码" align="center" prop="sydm" />
      <el-table-column label="启用状态" align="center" prop="status" >
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" @change="handleStatusChange(scope.row)" v-hasPermi="['archive:yqSy:editStatus']" :active-value='1' :inactive-value='0' >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="颜色" align="center" prop="color" />
      <el-table-column label="水印内容" align="center" prop="synr" />
      <el-table-column label="字体" align="center" prop="font" />
      <el-table-column label="字号" align="center" prop="fontsize" />
      <el-table-column label="透明度" align="center" prop="transparency" />
      <el-table-column label="多行水印" align="center" prop="line" />
      <el-table-column label="旋转水印" align="center" prop="rotate" />
<!--      <el-table-column label="创建人" align="center" prop="creatorId" />-->
<!--      <el-table-column label="创建时间" align="center" prop="createTime" />-->
<!--      <el-table-column label="最后修改人" align="center" prop="LASTUPDATER" />-->
<!--      <el-table-column label="最后修改时间" align="center" prop="lastupdateTime" />-->
<!--      <el-table-column label="动态水印显示方式" align="center" prop="TRENDS" />-->
<!--      <el-table-column label="是否删除" align="center" prop="isDel" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button

            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['archive:yqSy:edit']"
          >修改</el-button>
          <el-button

            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['archive:yqSy:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改水印对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="设置" prop="setup" >
              <el-select v-model="form.setup" style="width: 100%;" @change="setupChange(form)">
                <el-option v-for="dict in dict.type.ARCHIVE_YQSY_SETUP" :key="dict.value" :label="dict.label" :value="dict.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="水印内容" prop="synr" v-if="form.setup==0">
            <el-input ref="synrInput" v-model="form.staticValue" placeholder="请输入水印内容" @change="synrChange(form)"/>
          </el-form-item>
          <el-form-item label="水印内容" prop="synr" v-else>
            <el-select ref="synrSelect" v-model="form.dynamicValue" style="width: 100%;" @change="synrChange(form)">
              <el-option v-for="dict in dict.type.ARCHIVE_YQSY_TRENDS" :key="dict.value" :label="dict.label" :value="dict.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="水印代码" prop="sydm">
            <el-input ref="sydmInput" v-model="form.sydm" placeholder="请输入水印代码" style=""/>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="颜色" prop="color">
            <el-color-picker v-model="form.color" size="medium" color-format="rgb"></el-color-picker>
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="字体" prop="font">
              <el-select v-model="form.font" style="width: 100%;">
                <el-option v-for="dict in fontOption" :key="dict.value" :label="dict.label" :value="dict.value">
                </el-option>
<!--                <el-option :label="宋体" :value="1">-->
<!--                </el-option>-->
<!--                <el-option :label="仿宋" :value="2">-->
<!--                </el-option>-->
<!--                <el-option :label="黑体" :value="3">-->
<!--                </el-option>-->
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="字号" prop="fontsize">
              <el-select v-model="form.fontsize" style="width: 100%;">
                <el-option v-for="dict in fontsizeOption" :key="dict.value" :label="dict.label" :value="dict.value">
                </el-option>
<!--                <el-option :label="12" :value="0">-->
<!--                </el-option>-->
<!--                <el-option :label="14" :value="1">-->
<!--                </el-option>-->
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="透明度" prop="transparency">
            <el-slider :max='100' :step='1' v-model="form.transparency"></el-slider>
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="旋转水印" prop="rotate">
              <el-select v-model="form.rotate" style="width: 100%;">
                <el-option v-for="dict in dict.type.ARCHIVE_YQSY_ROTATE" :key="dict.value" :label="dict.label" :value="dict.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="多行水印" prop="line">
              <el-select v-model="form.line" style="width: 100%;">
                <el-option v-for="dict in dict.type.ARCHIVE_YQSY_LINE" :key="dict.value" :label="dict.label" :value="dict.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
var jtSynr = '';
var dtSynr = '';
import {listYqSy, getYqSy, delYqSy, addYqSy, updateYqSy, updateYQSYeditStatus} from "@/api/archive/yqSy";

export default {
  archive_Dicts: ['ARCHIVE_YQSY_SETUP','ARCHIVE_YQSY_LINE','ARCHIVE_YQSY_ROTATE','ARCHIVE_YQSY_TRENDS'],
  name: "yqSy",
  data() {
    const checkSydm = (rule, value, callback) => {
      if (value) {
        var reg = /^[a-zA-Z0-9][a-zA-Z0-9_]*$/;
        if (!reg.test(value)) {
          callback(new Error('格式是英文字母、数字或下划线,开头不能是下划线'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    function changeObjKey(key) {
      let message = {
        title: () => '请输入水印内容'
      }
      return message[key]()
    }
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 水印表格数据
      YqSyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
        color: null,
        setup: null,
        synr: null,
        sydm: null,
        font: null,
        fontsize: null,
        transparency: null,
        line: null,
        rotate: null,
        creatorId: null,
        createTime: null,
        lastupdater: null,
        lastupdateTime: null,
        trends: null,
        isDel: null
      },
      // 水印字体
      fontOption: [
        {value: '宋体',label: '宋体'},
        {value: '仿宋',label: '仿宋'},
        {value: '黑体',label: '黑体'},
        {value: '楷体',label: '楷体'}
      ],
      // 水印字号
      fontsizeOption: [
        {value: '12',label: '12'},
        {value: '14',label: '14'}
      ],
      // 表单参数
      form: {
        setup:'0',
        transparency:'50',
        font:'宋体',
        fontsize:'12',
        color:'0, 0, 0'
      },
      // 表单校验
      rules: {
        setup: [
          { required: true, message: "水印设置不能为空", trigger: "blur" }
        ],
        synr: [
          { required: true, message: "水印内容不能为空", trigger: "blur" }
        ],
        sydm: [
          { required: true, message: "水印代码不能为空", trigger: "blur" },
          { required: true, trigger: 'blur', validator: checkSydm }
        ],
        color: [
          { required: true, message: "颜色不能为空", trigger: "blur" }
        ],
        font: [
          { required: true, message: "字体不能为空", trigger: "blur" }
        ],
        fontsize: [
          { required: true, message: "字号不能为空", trigger: "blur" }
        ],
        transparency: [
          { required: true, message: "透明度不能为空", trigger: "blur" }
        ],
        rotate: [
          { required: true, message: "旋转水印不能为空", trigger: "blur" }
        ],
        line: [
          { required: true, message: "多行水印不能为空", trigger: "blur" }
        ]
      }
    };
  },
  // watch: {
  //   'form.setup'(newVal, oldVal) {
  //     this.form.synr = '';
  //     // 使用nextTick方法来确保Vue已经更新DOM后再判断form.setup的值来更新表单元素的显示状态
  //     this.$nextTick(() => {
  //       if (newVal === 0 || !newVal) {
  //         this.$el.querySelector('.el-input').style.display = 'block';
  //         this.$el.querySelector('.el-select').style.display = 'none';
  //       } else if (newVal === 1) {
  //         this.$el.querySelector('.el-input').style.display = 'none';
  //         this.$el.querySelector('.el-select').style.display = 'block';
  //       }
  //     });
  //   }
  // },
  created() {
    this.getList();
  },
  methods: {
    table_index(index){
      return (this.queryParams.pageNum-1) * this.queryParams.pageSize + index + 1;
    },
    /** 查询水印列表 */
    getList() {
      this.loading = true;
      listYqSy(this.queryParams).then(response => {
        this.YqSyList = response.rows;
        for (var i = 0; i < response.rows.length; i++) {
          //当setup=1时，渲染表格动态水印内容
          if (response.rows[i].setup == 1){
            for (let j = 0; j < this.dict.type.ARCHIVE_YQSY_TRENDS.length; j++) {
              if(response.rows[i].synr == this.dict.type.ARCHIVE_YQSY_TRENDS[j].value){
                this.YqSyList[i].synr = this.dict.type.ARCHIVE_YQSY_TRENDS[j].label
              }
            }
          }
          // 渲染表格水印设置
          for (let j = 0; j < this.dict.type.ARCHIVE_YQSY_SETUP.length; j++) {
            if(response.rows[i].setup == this.dict.type.ARCHIVE_YQSY_SETUP[j].value){
              this.YqSyList[i].setup = this.dict.type.ARCHIVE_YQSY_SETUP[j].label
            }
          }
          // 渲染表格水印多行
          for (let j = 0; j < this.dict.type.ARCHIVE_YQSY_LINE.length; j++) {
            if(response.rows[i].line == this.dict.type.ARCHIVE_YQSY_LINE[j].value){
              this.YqSyList[i].line = this.dict.type.ARCHIVE_YQSY_LINE[j].label
            }
          }
          // 渲染表格水印旋转
          for (let j = 0; j < this.dict.type.ARCHIVE_YQSY_ROTATE.length; j++) {
            if(response.rows[i].rotate == this.dict.type.ARCHIVE_YQSY_ROTATE[j].value){
              this.YqSyList[i].rotate = this.dict.type.ARCHIVE_YQSY_ROTATE[j].label
            }
          }
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        status: null,
        color: null,
        setup: null,
        synr: null,
        sydm: null,
        font: null,
        fontsize: null,
        transparency: null,
        line: null,
        rotate: null,
        creatorid: null,
        createtime: null,
        lastupdater: null,
        lastupdatetime: null,
        trends: null,
        isdel: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    //水印设置改变时，水印内容改变
    // onChange(data){
    //   console.log(data)
    //   if (data!="0"){
    //     const synrSelect = this.$refs.synrSelect;
    //     synrSelect.style.display = 'block';
    //   }else {
    //     const synrInput = this.$refs.synrInput;
    //     synrInput.style.display = 'block';
    //   }
    // },
    /** 新增按钮操作 */
    handleAdd() {
      jtSynr = "";
      dtSynr = "";
      this.reset();
      this.form={
        setup:'0',
        transparency:50,
        // font:'宋体',
        // fontsize:'12',
        color:'rgb(0, 0, 0)',
        // rotate:'0',
        // line:'0'
      };
      this.open = true;
      this.title = "添加水印";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      jtSynr = "";
      dtSynr = "";
      this.reset();
      const ID = row.id || this.ids
      getYqSy(ID).then(response => {
        this.form = response.data;
        this.form.setup = response.data.setup.toString();
        if(this.form.setup == 0) {
          this.$set(this.form, 'staticValue', this.form.synr)
        } else {
          this.$set(this.form, 'dynamicValue', this.form.synr)
        }
        this.form.rotate = response.data.rotate.toString();
        this.form.line = response.data.line.toString();
        this.open = true;
        this.title = "修改水印";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log(this.rules)
          if (this.form.id != null) {
            updateYqSy(this.form).then(response => {
              this.$modal.msgSuccess(response.msg);
              this.open = false;
              this.getList();
            });
          } else {
            addYqSy(this.form).then(response => {
              this.$modal.msgSuccess(response.msg);
              this.open = false;
              this.getList();
            });
          }
          jtSynr = '';
          dtSynr = '';
        }
      });
    },
    /** 修改水印启用状态 */
    handleStatusChange(row) {
      this.form = {
        id: row.id,
        status:row.status,
        sydm:row.sydm
      }
      // this.$modal.confirm('是否启用原词为"' + row.primary_word + '"的同义词？').then(function() {
      //
      // }).then(()=>{
      //   updateYQTYCeditStatus(this.form).then(response => {
      //     this.$modal.msgSuccess("修改成功！");
      //     this.getList();
      //   });
      // }).catch(() => {
      //   this.getList();
      // });
      updateYQSYeditStatus(this.form).then(response => {
        this.$modal.msgSuccess("修改成功！");
        this.getList();
      }).catch(() => {
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const IDs = row.id || this.ids;
      this.$modal.confirm('是否确认删除所选数据项？').then(function() {
        return delYqSy(IDs);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功!");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('archive/yqSy/export', {
        ...this.queryParams
      }, `YqSy_${new Date().getTime()}.xlsx`)
    },
    querySetupChange(queryParams) {
      // this.$nextTick(() => {
      queryParams.synr = null;
      // })
    },
    setupChange(form) {
      // this.$nextTick(() => {
        if (form.setup == '0'){
          dtSynr = form.synr;
          form.synr = jtSynr;
        }else {
          jtSynr = form.synr;
          form.synr = dtSynr;
        }
      // })
    },
    synrChange(form){
      if(form.setup == 0) {
        form.synr = form.staticValue
      } else {
        form.synr = form.dynamicValue
      }
    }
  }
};
</script>
